package nl.nhl.idp.Views.StatusView;

import java.util.ArrayList;
import nl.nhl.idp.R;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * ListView meant to show a status overview.
 * Set Items will be added, or updated if they already exist.
 * 
 * @author Alex Wijnbeek
 */
public class StatusListView extends ListView
{
	private ArrayList<ListViewItem> listArray;
	private ListItemAdapter adapter;
	private String colHeader1 = "";
	private String colHeader2 = "";
	
	/**
	 * Sets the font size to use in the statusview
	 * @param size
	 */
	public void SetTextSize(int size)
	{
		if(adapter != null)
			adapter.SetTextSize(size);
	}
	
	/**
	 * Creates a new StatusListView
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public StatusListView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		Initialize(context);
	}
	/**
	 * Creates a new StatusListView
	 * @param context
	 * @param attrs
	 */
	public StatusListView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}
	/**
	 * Creates a new StatusListView
	 * @param context
	 */
	public StatusListView(Context context)
	{
		this(context, null);
	}
	
	/**
	 * Initialize the components in View
	 * @param context
	 */
	private void Initialize(Context context)
	{
		//create data list
		listArray = new ArrayList<ListViewItem>();
		
		//add header item
		listArray.add(new ListViewItem(colHeader1, colHeader2));
		
		//initialize adapter
		adapter = new ListItemAdapter(context, R.layout.listviewitem, listArray);
		setAdapter(adapter);
	}
	
	/**
	 * Set column header text, if not defined, column headers will be empty
	 * @param headerIndex
	 * @param headerText
	 */
	public void SetColHeader(int headerIndex, String headerText)
	{
		if(headerIndex == 0)
			colHeader1 = headerText;
		else
			colHeader2 = headerText;
		if(listArray != null)
		{
			listArray.set(0, new ListViewItem(colHeader1, colHeader2));
		}
	}
	
	/**
	 * Sets an item with the given name and value
	 * If an item with the given name already exists, it's value will be updated to the given value
	 * @param name
	 * @param value
	 */
	public void SetItem(String name, Object value)
	{
		this.SetItem(name, value, "");
	}
	
	/**
	 * Sets an item with the given name, value and unit
	 * If an item with the given name already exists, it's value will be updated to the given value
	 * @param name
	 * @param value
	 * @param unit
	 */
	public void SetItem(String name, Object value, String unit)
	{
		boolean exist = false;
		for(int i=0;i<listArray.size();i++)
		{
			if(listArray.get(i)!=null && listArray.get(i).GetName().equals(name))
			{
				exist = true;
				listArray.get(i).SetValue(value);
				break;
			}	
		}
		if(!exist)
			listArray.add(new ListViewItem(name, value, unit));
		
		adapter.notifyDataSetChanged();
	}
}
